package com.boot_reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot_reggie.Service.impl.AdressServiceImpl;
import com.boot_reggie.Service.impl.UserServiceImpl;
import com.boot_reggie.common.Result;
import com.boot_reggie.domain.AddressBook;
import com.boot_reggie.domain.User;
import com.boot_reggie.filter.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class CustomerConttroller {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private AdressServiceImpl adressService;
    @PostMapping
    public Result<String> register(@RequestBody Map map, HttpSession session){
        System.out.println("验证码为:"+session.getAttribute("code"));
        System.out.println(map);
        User user = new User();
//        System.out.println(user.getCode());
        user.setStatus(1);
        user.setAvatar(map.get("avatar").toString());
        user.setName(map.get("name").toString());
        user.setMailbox(map.get("mailbox").toString());
        user.setIdNumber(map.get("idNumber").toString());
        user.setSex(map.get("sex").toString());
        user.setPhone(map.get("phone").toString());
        if(map.get("code").equals(session.getAttribute("code"))){
            System.out.println(user);
        }else {
            return Result.error("验证码错误");
        };
        LambdaQueryWrapper<User> lwq = new LambdaQueryWrapper<>();
        lwq.eq(User::getMailbox,user.getMailbox());

        int count = userService.count(lwq);
        if(count > 0){
            return Result.error("邮箱已经存在");
        }
        boolean  data= userService.save(user);
        if(data){
            return Result.success("注册成功");
        }else {
            return Result.error("注册失败");
        }

    }
    @PostMapping("login")
    public Result<String> login(@RequestBody Map map,HttpSession session){
        if(map.get("code").equals(session.getAttribute("code"))){
            System.out.println(map);
            LambdaQueryWrapper<User> lwq = new LambdaQueryWrapper<>();
            lwq.eq(User::getPhone,map.get("phone"));
            User user = userService.getOne(lwq);
            session.setAttribute("userInfo",user);
            BaseContext.setThreadLocal1(user.getId());
            return Result.success("登录成功");
        }
        return Result.error("验证码错误");
    }

    @GetMapping("test")
    public Result<String> test(HttpSession session){
        if(session.getAttribute("userInfo") != null){
            return Result.success("已登陆");
        }else {
            return Result.error("未登录");
        }
    }

    @GetMapping("adress")
    public Result<User> adress(HttpSession session){
        System.out.println(session.getAttribute("userInfo") != null);
        if(session.getAttribute("userInfo") != null){
            User user = (User) session.getAttribute("userInfo");
            User user1 = userService.getById(user.getId());
            return Result.success(user1,"数据查询成功");
        };
        return Result.error("后端接口出错404");
    }
    @GetMapping("exit")
    public Result<String> Exit(HttpSession session){
        session.setAttribute("userInfo",null);
        return Result.success("退出成功");
    }

//    添加地址
    @PostMapping("Adress")
    public Result<String> Adress(@RequestBody AddressBook addressBook, HttpSession session){
        User user = (User)session.getAttribute("userInfo");
        addressBook.setUserId(user.getId());
        addressBook.setIsDefault(0);
        boolean data = adressService.save(addressBook);
        if(data){
            return Result.success("地址添加成功");
        }else {
            return Result.error("地址添加失败");

        }
    }

    /*获取地址*/
    @GetMapping("Adress")
    public Result<List<AddressBook>> listResult (HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        LambdaQueryWrapper<AddressBook> lwq = new LambdaQueryWrapper<>();
        lwq.eq(AddressBook::getIsDeleted,0);
        lwq.eq(AddressBook::getUserId,user.getId());
        List<AddressBook> list = adressService.list(lwq);
        return Result.success(list,"数据获取成功");
    }
    @GetMapping("id/{id}")
    public Result<String> updateAdress(@PathVariable Long id,HttpSession session){
        System.out.println(id);
        User user = (User) session.getAttribute("userInfo");
//        获取名下所有的id
        LambdaQueryWrapper<AddressBook> lwq = new LambdaQueryWrapper<>();
        lwq.eq(AddressBook::getUserId,user.getId());
        List<AddressBook> list = adressService.list(lwq);
        for (AddressBook addressBook : list) {
            addressBook.setIsDefault(0);
        }
        /*根据所有id将地址改为默认*/
        adressService.updateBatchById(list);
        /*修改指定的默认*/
        LambdaQueryWrapper<AddressBook> lwq1 = new LambdaQueryWrapper<>();
        lwq1.eq(AddressBook::getId,id);
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        adressService.update(addressBook,lwq1);
        return Result.success("修改成功");
    };
    @PutMapping("Adress")
    public Result<String> AdressEdit(@RequestBody AddressBook addressBook,HttpSession session){
        if(addressBook.getIsDefault() == 1){
            User user = (User) session.getAttribute("userInfo");

            LambdaQueryWrapper<AddressBook> lwq = new LambdaQueryWrapper<>();
            lwq.eq(AddressBook::getUserId,user.getId());
            List<AddressBook> list = adressService.list(lwq);
            for (AddressBook addressBooks : list) {
                addressBooks.setIsDefault(0);
            }
            /*根据所有id将地址改为默认*/
            adressService.updateBatchById(list);
            adressService.updateById(addressBook);
            return Result.success("修改成功");
        }else {
            adressService.updateById(addressBook);
            return Result.success("修改成功");
        }

    };
    @GetMapping("Adress/{id}")
    public Result<AddressBook> getAddress(@PathVariable Long id,HttpSession session){
        return Result.success(adressService.getById(id),"获取成功");
    }
    @DeleteMapping("Adress/{id}")
    public Result<String> AdressDelete(@PathVariable Long  id){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDeleted(1);
        addressBook.setId(id);
        adressService.updateById(addressBook);
        return Result.success("删除成功");
    }
}
